<?
/*! \brief Refreshes cluster loads
 * 
 */
class LoadBalancer 
{	
	public $_db;
	
	public function __construct()
	{
		$this->_db = Zend_Registry::get('db');
	}
	
	public function refreshClusterLoads()
	{
		$root = new Root(HOST_NAME);
		$list = $root->getMasterList();
		
		if($list)
		{
			foreach($list as $master)
			{
				$m = new Master($master->addr);				
				$load = $m->getLoad();
				$root->setClusterLoad($master->addr, $load);
			}
		}
	}
	
	public function refreshSlaveLoads()
	{	
		$master = new Master(HOST_NAME);
		$list = $master->getSlaveList();
		

		if($list)
		{
			foreach($list as $slave)
			{
				$s = new Slave($slave->addr);
				$load = $s->getLoad();
				$master->setSlaveLoad($slave->addr, $load);
			}
		}	
	}	
	
	public function getClusterLeastLoad()
	{
		$root = new Root(HOST_NAME);
		$cluster = $root->getClusterLeastLoad();
		
		return $cluster;
	}
	
	public function getSlaveLeastLoad()
	{
		$master = new Master(HOST_NAME);
		$slave = $master->getSlaveLeastLoad();
		
		return $slave;
	}
}